package com.atguigu.gmall.pms.controller;

import java.util.List;

import com.atguigu.gmall.pms.entity.vo.SpuVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.SpuEntity;
import com.atguigu.gmall.pms.service.SpuService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * spu信息
 *
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2023-02-09 10:16:38
 */
@Api(tags = "spu信息 管理")
@RestController
@RequestMapping("pms/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;


    @GetMapping("/publish/{status}")
    @ApiOperation("分页查询已发布的Spu列表")
    public ResponseVo<List<SpuEntity>> queryPublishSpuByPage(PageParamVo paramVo,

                  @ApiParam("发布状态: 1已发布，0未发布") @PathVariable("status")Integer status){
        List<SpuEntity> spuEntities = spuService.queryPublishSpuByPage(paramVo,status);
        return ResponseVo.ok(spuEntities);
    }

    /*
        传参方式：
            1、路径参数：/category/{categoryId}
                    获取：@PathVariable("categoryId")Long categoryId
            2、请求参数： url??t=1676081252594&pageNum=1&pageSize=10&key=
                    获取：
                        @ReuqestParam("pageNum") Integer pageNum
                        pojo入参：   PageParamVo paramVo
            3、get以外的请求方式+请求体参数：
                    文件参数：  MultipartFile file
                    json字符串：@RequestBody PageParamVo paramVo

     */
    // /category/0?t=1676081252594&pageNum=1&pageSize=10&key=
    @ApiOperation("带条件查询分类下的spu分页数据")
    @GetMapping("/category/{categoryId}")
    public ResponseVo listByCondition(@PathVariable("categoryId")Long categoryId,
                                      PageParamVo paramVo){
        PageResultVo pageResultVo = spuService.listByCondition(categoryId,paramVo);
        return ResponseVo.ok(pageResultVo);
    }
    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> querySpuByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = spuService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<SpuEntity> querySpuById(@PathVariable("id") Long id){
		SpuEntity spu = spuService.getById(id);

        return ResponseVo.ok(spu);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody SpuVO spuVO){
		spuService.bigSave(spuVO);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody SpuEntity spu){
		spuService.updateById(spu);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		spuService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
